from fvutil import fvcon

class ObjectBroker:
    
    def __init__(self, presence_wrapper, group, audio, text, poll, shopping, tivo):
        
        self.__on_open_handlers = {
                fvcon.AUDIO : audio.get,
                fvcon.TEXT : text.get,
                fvcon.POLL_Q : poll.get_poll,
                fvcon.POLL_R : poll.get_result,
                fvcon.SHOPPING : shopping.get,
                fvcon.FV : presence_wrapper.get,
                fvcon.PHONE : presence_wrapper.get,
                fvcon.SERVICE : presence_wrapper.get,
                fvcon.TIVO : tivo.get,
        }
        """
        self.__on_edit_handlers = {
                fvcon.TEXT : text.get_text_details,
                fvcon.POLL_Q : poll.get_poll_details,
                fvcon.POLL_R : poll.get_poll_result_details,
        }
        """
        self.__on_edit_handlers = {}
        
        self.__on_send_handlers = {
                fvcon.AUDIO : audio.send,
                fvcon.TEXT : text.send,
                fvcon.POLL_Q : poll.send_question,
                fvcon.POLL_V : poll.send_vote,
                fvcon.SHOPPING : shopping.send,
        }
        
        self.__on_receive_handlers = {
                fvcon.AUDIO : audio.receive,
                fvcon.TEXT : text.receive,
                fvcon.POLL_Q : poll.receive_question,
                fvcon.POLL_V : poll.receive_vote,
                fvcon.POLL_R : poll.receive_result,
                fvcon.SHOPPING : shopping.receive,
                fvcon.TIVO : tivo.receive,
        }
        
        self.__get_all_handlers = (text.get_all,
                                   audio.get_all,
                                   poll.get_all,
                                   group.get_all,
                                   shopping.get_all,
                                   presence_wrapper.get_all,
                                   tivo.get_all)
        
    def on_open_object(self, obj, oid, o_type):
        print 'Broker: On_open_object',oid,o_type
        handler = self.__on_open_handlers.get(o_type, None)
        if handler:
            #return handler(oid)
            return obj.returnObject(handler(oid))
        else:
            raise ArgumentError('No open handler defined for object type ' + o_type)
    
    def on_edit_object(self, obj, params):
        # Not clear that this is used.
        pass
    
    def on_send_object(self, obj, params):
        handler = self.__on_send_handlers.get(params['type'], None)
        if handler:
            handler(params)
        else:
            raise KeyError('No send handler defined for object type ' + str(params['type']))
        
    def on_receive_object(self, obj, header):
        print 'received message type',header.msg_type
        handler = self.__on_receive_handlers.get(header.msg_type, None)
        if handler:
            handler(header)
        else:
            raise KeyError('No receive handler defined for object type ' + str(header.msg_type))
        
    def get_all_objects(self, obj=None, data=None):
        # obj, data exist if this is signal callback
        objs = []
        for handler in self.__get_all_handlers:
            objs += handler()
        print objs
        return objs